package cn.ant.Client;

import cn.ant.utils.ChannelUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalTime;


/**
 * 生产者（生产任务）
 *
 * @author Anhui AntLaddie <a href="https://juejin.cn/user/4092805620185316">(掘金蚂蚁小哥)</a>
 * @version 1.0
 **/
public class Producer {

    //通过日志管理器获取Logger对象
    static Logger logger = LogManager.getLogger(Producer.class);
    // 扇出交换机名称
    public static final String EXCHANGE_NAME = "fanoutExchange";

    public static void main(String[] args) throws IOException {
        // 通过工具类获取一个信道
        Channel channel = ChannelUtil.getChannel();
        // 创建名称为"fanoutExchange"的扇出交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT,
                true, false, false, null);

        // 循环发送消息（循环20条发生）
        for (int i = 1; i <= 20; i++) {
            String str = "这是一个编号为：" + i + " 的待处理的消息";
            byte[] msg = (str + LocalTime.now()).getBytes(StandardCharsets.UTF_8);
            //发送消息到交换机 EXCHANGE_NAME
            channel.basicPublish(EXCHANGE_NAME, "", MessageProperties.PERSISTENT_BASIC, msg);
        }
        logger.info("生产者消息发送完成！");
    }
}
